﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Master/SiteContent.Master" AutoEventWireup="true" CodeBehind="Authority.aspx.cs" Inherits="Warehouse.Web.Management.Authority" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        .moduleheader .moduleheadertoolbar > * {
            margin: 16px 5px 0 5px;
        }
        ul
        {
            margin-left:20px;
            }
        .panelTree
        {
            width:240px;
            float:left;
            }
        #content
        {
            float:left;
            margin-top:10px;
            font-size:14px;
            }
        tr
        {
            height:30px;
            }
        #default
        {
            font-size:14px;
            color:Red;
            }
        #depTree .icon-dep
        {
            color:#4890CE;
            width:17px;
            height:18px;
            display:inline-block;
            cursor:pointer;
            margin-top:5px;
            font-size:14px;
            background:url(../styles/images/ui-icons_2e83ff_256x240.png) -63px -188px no-repeat;
            }
        #depTree .close
        {
            background:url(../styles/images/ui-icons_2e83ff_256x240.png) -48px -188px no-repeat;
            }
        #depTree .li-treeitem
        {
            color:#4890CE;
            margin-left:8px;
            cursor:pointer;
            font-size:14px;
            }
        .userli
        {
            padding-left:10px;
            margin-top:5px;
            cursor:pointer;
            }
        .contextmenu
        {
            z-index:1000;
            position:absolute;
            display:none;
            border:1px solid #ccc;
            border-radius:3px;
            padding:5px;
            background-color:#F4FDFC;
            margin-left:10px;
            }
        .contextmenu li
        {
            border-bottom:1px solid #ccc;
            text-align:center;
            cursor:pointer;
            line-height:20px;
            }
        .hide
        {
            display:none;
            }
        #targetname
        {
            color:#2C82FC;
            font-weight:bold;
            }
        #permission
        {
            line-height:25px;
            }
        #permission .pmtitle
        {
            display:inline-block;
            min-width:80px;
            }
    </style>
    <script type="text/javascript">
        var curId = 0;
        var curName = '';
        var curType = '';
        var needHide = true;
        function hideAll() {
            $("#default").hide();
            $("#department_add").hide();
            $("#department_edit").hide();
            $("#user").hide();
            $("#permission").hide();
        }
        //添加部门
        function addDepartment() {
            var name = $.trim($("#departmentName_add").val());
            var parentID = $("#parentDep").data("did");
            if (name) {
                Util.post('./Authority.aspx', { action: 'AddDepartment', DepartmentName: name, ParentID: parentID }, function (succeed, data, err) {
                    if (succeed) {
                        alert("添加成功");
                        var $depUl = $("#depTree").find("span[data-did=" + parentID + "]").next();
                        var $firstUser = $depUl.children("li.userli");
                        if ($firstUser.length == 0) {
                            //$("<span class='icon-dep'></span><span class='li-treeitem' data-did='5'>财务子部1</span><ul></ul>")
                            //.appendTo($depUl);
                            $depUl.append($.format("<li><span class='icon-dep'></span><span class='li-treeitem' data-did='{0}'>{1}</span><ul></ul></li>", data.DepartmentID, name));
                        }
                        else {
                            $firstUser.before($.format("<li><span class='icon-dep'></span><span class='li-treeitem' data-did='{0}'>{1}</span><ul></ul></li>", data.DepartmentID, name));
                        }
                    }
                    else {
                        alert(err);
                    }
                });
            }
        }
        //编辑部门
        function editDepartment() {
            var name = $.trim($("#departmentName").val());
            if (name) {
                var did = $("#departmentName").data("did");
                Util.post('./Authority.aspx', { action: 'EditDepartment', DepartmentName: name, ID: did }, function (succeed, data, err) {
                    if (succeed) {
                        alert("更新成功");
                        $("#depTree").find("span[data-did=" + did + "]").text(name);
                    }
                    else {
                        alert(err);
                    }
                });
            }
        }
        //删除部门
        function delDepartment() {
            if (confirm("删除部门的同时会删除该部门下所有员工，确定删除？")) {
                Util.post('./Authority.aspx', { action: 'DelDepartment', ID: curId }, function (succeed, data, err) {
                    if (succeed) {
                        alert("删除成功");
                        $("#depTree").find("span[data-did=" + curId + "]").parent().remove();
                    }
                    else {
                        alert(err);
                    }
                });
            }
        }
        //删除员工
        function delUser() {
            if (confirm("确定删除该员工？")) {
                Util.post('./Authority.aspx', { action: 'DelUser', ID: curId }, function (succeed, data, err) {
                    if (succeed) {
                        alert("删除成功");
                        $("#depTree").find("li[data-uid=" + curId + "]").remove();
                    }
                    else {
                        alert(err);
                    }
                });
            }
        }
        function updateUser() {
            var username = $.trim($("#username").val());
            var loginname = $.trim($("#loginname").val());
            var password = $.trim($("#password").val());
            if (username && loginname && password) {
                var telephone = $.trim($("#telephone").val());
                var mobilephone = $.trim($("#mobilephone").val());
                var gender = true;
                if ($("#female").attr("checked")) {
                    gender = false;
                }
                var aid = $("#actiontype").data("aid");
                //添加员工
                if ($("#actiontype").val() == "add") {
                    Util.post('./Authority.aspx', { action: 'AddUser', Name: username, LoginName:loginname, Password:password, Telephone: telephone, MobilePhone: mobilephone, Gender: gender, DepartmentID: aid }, function (succeed, data, err) {
                        if (succeed) {
                            alert("添加成功");
                            $("#depTree").find("span[data-did=" + aid + "]").next().append($.format("<li class='userli' data-uid='{0}'>{1}</li>", data.UserID, username));
                            clearUserParams();
                        }
                        else {
                            alert(err);
                        }
                    });
                }
                else {
                    //编辑员工
                    if ($("#changepwd").attr("checked") != 'checked') {
                        password = '';
                    }
                    Util.post('./Authority.aspx', { action: 'EditUser', Name: username,LoginName:loginname,Password:password, Telephone: telephone, MobilePhone: mobilephone, Gender: gender, ID: aid }, function (succeed, data, err) {
                        if (succeed) {
                            $("#default").html(data).show();
                            alert("更新成功");
                            $("#depTree").find("li[data-uid=" + aid + "]").text(username);
                            //clearUserParams();
                        }
                        else {
                            alert(err);
                        }
                    });
                }
            }
            else {
                alert("姓名、登录名、密码都不能为空");
            }
        }
        //清除员工信息区域输入框
        function clearUserParams() {
            $("#username").val("");
            $("#loginname").val("");
            $("#password").val("");
            $("#telephone").val("");
            $("#mobilephone").val("");
            $("#actiontype").val("").data("aid", "");
        }
        function clearAllPermissions() {
            $("#permission input[type=checkbox]").removeAttr("checked").attr({ "disabled": false });
        }

        //获取权限
        function getPermissions(id, type) {
            Util.get('./Authority.aspx', { action: 'GetPermissions', ID: id, Type: type }, function (succeed, data, err) {
                if (succeed) {
                    clearAllPermissions();
                    $.each(data, function () {
                        if ((type == 'u' && this.TargetType == 'd') || (type == 'd' && this.TargetID !=id)) {
                            $("#p_" + this.PermissionID).attr({ "checked": "checked", "disabled": true });
                        }
                        else {
                            $("#p_" + this.PermissionID).attr("checked", "checked");
                        }
                    });
                    $("#btn_permission").attr("disabled", false);
                }
                else {
                    alert(err);
                }
            });
        }

        //保存权限设置
        function savePermission() {
            var targetId = $("#targetid").val();
            var targetType = $("#targetid").data("targettype");
            var permissions = '';

            $("#permission input[type=checkbox]").each(function () {
                if ($(this).attr("checked") == "checked" && $(this).attr("disabled") != "disabled") {
                    permissions += $(this).attr("id").substring(2) + ",";
                }
            });

            Util.post('./Authority.aspx', { action: "SavePermission", TargetID: targetId, TargetType: targetType, Permissions: permissions }, function (succeed, data, err) {
                if (succeed) {
                    alert("编辑权限成功");
                }
                else {
                    alert(err);
                }
            });
        }

        $(function () {
            $(document).live("contextmenu click", function () {
                if (needHide) {
                    $("#contextMenu_dep").hide();
                    $("#contextMenu_user").hide();
                }
                needHide = true;
            });
            //展开折叠
            $("#depTree span.icon-dep").live("click", function () {
                if ($(this).hasClass("close")) {
                    $(this).removeClass("close").parent().children("ul").show();
                }
                else {
                    $(this).addClass("close").parent().children("ul").hide();
                }
            });
            //部门右键菜单
            $("#depTree span.li-treeitem").live("contextmenu", function (e) {
                e.preventDefault();
                $("#contextMenu_user").hide();
                var left = $(this).position().left + $(this).width();
                $("#contextMenu_dep").show().css({ 'left': left, 'top': $(this).position().top });
                curId = $(this).data("did");
                curName = $(this).text();
                curType = "d";
                needHide = false;
            });
            //员工右键菜单
            $("#depTree li.userli").live("contextmenu", function (e) {
                e.preventDefault();
                $("#contextMenu_dep").hide();
                var left = $(this).position().left + $(this).width();
                $("#contextMenu_user").show().css({ 'left': left, 'top': $(this).position().top });
                curId = $(this).data("uid");
                curName = $(this).text();
                curType = "u";
                needHide = false;
            });
            //右键菜单选项
            $("#contextMenu_dep li,#contextMenu_user li").click(function () {
                var operation = $(this).text();
                switch (operation) {
                    case '添加部门':
                        hideAll();
                        $("#department_add").show();
                        $("#parentDep").text(curName).data('did', curId);
                        break;
                    case '编辑':
                        hideAll();
                        //编辑部门
                        if (curType == "d") {
                            $("#department_edit").show();
                            $("#departmentName").val(curName).data('did', curId);
                        }
                        else {
                            //编辑员工
                            clearUserParams();
                            $("#password").attr("disabled", true).next().show().next().show(); ;
                            $("#actiontype").val("edit").data("aid", curId);
                            Util.get('./Authority.aspx', { action: 'GetUser', ID: curId }, function (succeed, data, err) {
                                if (succeed) {
                                    $("#user").show();
                                    $("#loginname").val(data.LoginName);
                                    $("#username").val(data.Name);
                                    $("#telephone").val(data.Telephone);
                                    $("#mobilephone").val(data.MobilePhone);
                                    $("#password").val("111111");
                                    $("#changepwd").removeAttr("checked");
                                    if (data.Gender) {
                                        $("#male").attr("checked", "checked");
                                    }
                                    else {
                                        $("#female").attr("checked", "checked");
                                    }
                                    $("#userdepartment").text($("#depTree").find("span[data-did=" + data.DepartmentID + "]").text());
                                }
                                else {
                                    alert(err);
                                }
                            });
                        }
                        break;
                    case '删除':
                        hideAll();
                        if (curType == 'd') {
                            delDepartment();
                        }
                        else {
                            delUser();
                        }
                        break;
                    case '添加员工':
                        hideAll();
                        $("#user").show();
                        clearUserParams();
                        $("#actiontype").val("add").data("aid", curId);
                        $("#password").attr("disabled", false).val('').next().hide().next().hide(); ;
                        $("#userdepartment").text($("#depTree").find("span[data-did=" + curId + "]").text());
                        break;
                    case '权限':
                        hideAll();
                        $("#btn_permission").attr("disabled", true);
                        getPermissions(curId, curType);
                        $("#targetname").text(curName);
                        $("#permission").show();
                        $("#targetid").val(curId).data("targettype", curType);
                        break;
                }
            });

            if ($.browser.msie) {
                $("#changepwd").click(function () {
                    this.blur();
                    this.focus();
                });
            }

            $("#changepwd").change(function () {
                if ($(this).attr("checked")) {
                    $("#password").val('').attr("disabled", false);
                }
                else {
                    $("#password").val('111111').attr("disabled", true);
                }
            });
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="moduleheader">
        <h4>
            权限管理</h4>
        <div class="moduleheadertoolbar">
            <input type="button" id="btnRefresh" class="ui-button-refresh" onclick="window.location.reload();" value="刷新" />
        </div>
    </div>
    <div class="modulecontent">
        <ul id="contextMenu_dep" class="contextmenu">
            <li>添加部门</li>
            <li>添加员工</li>
            <li>权限</li>
            <li>编辑</li>
            <li style="border:none;">删除</li>
        </ul>
        <ul id="contextMenu_user" class="contextmenu">
            <li>权限</li>
            <li>编辑</li>
            <li style="border:none;">删除</li>
        </ul>
        <asp:Panel ID="panelTree" runat="server" CssClass="panelTree"></asp:Panel>
        <div id="content">
            <div id="default">
                右键点击部门或者员工名称显示操作菜单。
            </div>
            <div id="department_add" class="hide">
                <table>
                    <tr>
                        <td>上级部门：</td>
                        <td><span id="parentDep"></span></td>
                    </tr>
                    <tr>
                        <td>部门名称：</td>
                        <td><input type="text" id="departmentName_add" maxlength="20" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <center>
                                <input type="button" value="确定" onclick="addDepartment();" class="ui-button" />
                            </center>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="department_edit" class="hide">
                <table>
                    <tr>
                        <td>部门名称：</td>
                        <td><input type="text" id="departmentName" maxlength="20" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <center>
                                <input type="button" value="确定" onclick="editDepartment();" class="ui-button" />
                            </center>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="user" class="hide">
                <table>
                    <tr>
                        <td>所属部门：</td>
                        <td><span id="userdepartment"></span></td>
                    </tr>
                    <tr>
                        <td>登录名：</td>
                        <td><input type="text" id="loginname" /></td>
                        
                    </tr>
                    <tr>
                        <td>密码：</td>
                        <td><input type="password" id="password" style="width:153px;" /> <input type="checkbox" id="changepwd" onchange="changepwd();" /><span> 重置密码</span></td>
                        
                    </tr>
                    <tr>
                        <td>姓名：</td>
                        <td><input type="text" id="username" /><input type="hidden" id="actiontype" /></td>
                        
                    </tr>
                    <tr>
                        <td>性别：</td>
                        <td>
                            <input type="radio" value="1" checked="checked" id="male" name="gender"/>男
                            <input type="radio" value="2" id="female" name="gender" />女
                        </td>
                    </tr>
                    <tr>
                        <td>手机：</td>
                        <td><input type="text" id="mobilephone" /></td>
                        
                    </tr>
                    <tr>
                        <td>座机：</td>
                        <td><input type="text" id="telephone" /></td>
                    </tr>
                    <tr>                    
                        <td colspan="2">
                            <center>
                                <input type="button" value="确定" onclick="updateUser();" class="ui-button" />
                            </center>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="permission" class="hide">
                <input type="hidden" id="targetid" />
                <p>
                    <span id="targetname"></span> 的权限：
                </p>
                <asp:Panel ID="permissionPanel" runat="server"></asp:Panel>
                <p>                    
                    <center>
                    <input type="button" value="确定" id="btn_permission" onclick="savePermission();" class="ui-button" />
                    </center>
                </p>
            </div>
        </div>
    </div>
</asp:Content>
